<?php
namespace app\common\util;

define('AppKey', '');
define('Secret', '');

class HttpClient {

    //获取AccessToken
    function getToken(){
        $msgInfo = array(
            'appKey'=>AppKey,
            'appSecret'=>Secret,
        );
        $url='https://open.ys7.com/api/lapp/token/get';
        return $this->getResult($url, $msgInfo);
    }

    //添加监控设备
    function addMonitor($deviceSerial,$validateCode){
        $getToken=$this->getToken();
        if($getToken['code']!=200){
            return json(['code'=>0,'msg'=>$getToken['msg']]);
        }
        $accessToken=$getToken['data']['accessToken'];

        $msgInfo = array(
            'accessToken'=>$accessToken,
            'deviceSerial'=>$deviceSerial,
            'validateCode'=>$validateCode,
        );
        $url='https://open.ys7.com/api/lapp/device/add';
        return $this->getResult($url, $msgInfo);
    }

    //删除监控设备
    function deleteMonitor($deviceSerial){
        $getToken=$this->getToken();
        if($getToken['code']!=200){
            return json(['code'=>0,'msg'=>$getToken['msg']]);
        }
        $accessToken=$getToken['data']['accessToken'];

        $msgInfo = array(
            'accessToken'=>$accessToken,
            'deviceSerial'=>$deviceSerial,
        );
        $url='https://open.ys7.com/api/lapp/device/delete';
        return $this->getResult($url, $msgInfo);
    }

    //获取监控地址
    function getAddress($deviceSerial,$code){
        $getToken=$this->getToken();
        if($getToken['code']!=200){
            return json(['code'=>0,'msg'=>$getToken['msg']]);
        }
        $accessToken=$getToken['data']['accessToken'];

        $msgInfo = array(
            'accessToken'=>$accessToken,
            'deviceSerial'=>$deviceSerial,
            'code'=>$code,
        );
        $url='https://open.ys7.com/api/lapp/v2/live/address/get';
        return $this->getResult($url, $msgInfo);
    }

    /**
     * [批量删除打印机 Open_printerDelList]
     * @param  [string] $snlist [打印机编号，多台打印机请用减号“-”连接起来]
     * @return [string]         [接口返回值]
     */
    function printerDelList($snlist){
        $time = time();         //请求时间
        $msgInfo = array(
            'user'=>USER,
            'stime'=>$time,
            'sig'=>$this->signature($time),
            'apiname'=>'Open_printerDelList',
            'snlist'=>$snlist
        );
        return $this->getResult(URL, $msgInfo);
    }

    /**
     * [打印订单接口 Open_printMsg]
     * @param  [string] $sn      [打印机编号sn]
     * @param  [string] $content [打印内容]
     * @param  [string] $times   [打印联数]
     * @return [string]          [接口返回值]
     */
    function printMsg($sn,$content,$times=1){
        $time = time();         //请求时间
        $msgInfo = array(
            'user'=>USER,
            'stime'=>$time,
            'sig'=>$this->signature($time),
            'apiname'=>'Open_printMsg',
            'sn'=>$sn,
            'content'=>$content,
            'times'=>$times//打印次数
        );
        return $this->getResult(URL, $msgInfo);
    }

    /**
     * [signature 生成签名]
     * @param  [string] $time [当前UNIX时间戳，10位，精确到秒]
     * @return [string]       [接口返回值]
     */
    function signature($time){
        return sha1(USER.UKEY.$time);//公共参数，请求公钥
    }

    //返回数据结果集
    private function getResult($url, $body = null)
    {
        $data = $this->connection($url,$body);
        if (isset($data) && !empty($data)) {
            $result = $data;
        } else {
            $result = '没有返回数据';
        }
        return $result;
    }

    /**
     * @param $url    请求链接
     * @param $body   post数据
     * @return mixed|string
     */
    private function connection($url, $body){

        $headers = array('Content-Type: application/x-www-form-urlencoded');
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            return 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return json_decode($result,true);
    }
}

?>